import { AjaxService, Req, Res } from "./ajax.service"
import { Injectable } from '@angular/core'

import { Stage } from "./stage"

@Injectable()
export class StageService {
  private bh = AjaxService.APIROOT + '/projects';

  getList(project_id): Promise<Array<Stage>> {
    var stages: Array<Stage> = [ ]
    return new Promise<Array<Stage>>(resolve => {
        AjaxService.request(new Req(`${this.bh}/${project_id}/stages.json`, "GET"))
          .then(res => {
            if (!res.error)
              res.data.map(row => { stages.push(this.dataInit(row)) })
            resolve()
          }).catch((err: any) => { console.log("get stage error", err) })
      }).then(() => stages)
  }

  private dataInit(row: any): Stage {
    row["tips"] = row["tips"].split(",")
    return row
  }
}
